package cn.jbit.test;

import java.io.*;

/**
 *  临时代码，主要作用是读取日志文件，将多个文件合并成一个大文件，另外组织成json格式数据输出
 *  运行之前首先手动删除D:\\log.txt文件
 */
public class MergeLogFileToOneFile {
    public static void main(String args) throws IOException {
        File file = new File("D:\\2018IDEA_space\\RtMusicProject\\GenerateData\\src\\main\\properties\\clientlog");
        File[] fs = file.listFiles();
        //遍历File[]数组
        for(File f:fs){
            //获取文件和目录名
            String fileName = f.getName();
            if (!f.isDirectory()) {
                BufferedReader reader = null;
                BufferedWriter writer = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
                    writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:\\userlog_songplay.txt"),true),"UTF-8"));
                    String line;
                    // 一次读入一行，直到读入null为文件结束
                    while( (line = reader.readLine())!=null){
                        System.out.println(line);
                        //解析数据，将每行数据修改成json格式数据写出
                        String[] split = line.split("&");
                        if(split.length==6&&split[2].equals("MINIK_CLIENT_SONG_PLAY_OPERATE_REQ")){
                            line = "{\"timestamp\":\""+split[0]+"\",\"mid\":\""+split[1]+"\",\"event_type\":\""+split[2]+"\"," +
                                    "\"event_content\":"+split[3]+",\"ui_version\":\""+split[4]+"\",\"machine_version\":\""+split[5]+"\"}";
                            writer.write(line);
                            writer.newLine();
                            writer.flush();
                        }

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                            writer.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }

            }
        }
    }
}
